#include "Player.h"

Player::Player( const Position& pos )
{
	setName("Player");
	setPosition(pos);

	init();
}

Player::Player()
{
	init();
	m_endJumpTime = 0;
	m_jumping = false;
	m_intersecting = false;
	m_dead = false;
	m_haswon = false;
}

void Player::frame( float frametime)
{
	
	//jump
	Vector temp =  this->getVelocity();
	if(this->getVelocity().y >= 0){ //wanneer player niet aan het vallen is
		if(m_movement.jump && (SDL_GetTicks() < m_endJumpTime || m_endJumpTime == 0)){ //jump is ingedrukt binnen jumptime
			temp.y = (m_endJumpTime - SDL_GetTicks()) *0.25;
			m_jumping = true;
			if(m_endJumpTime == 0)
				m_endJumpTime = SDL_GetTicks() + 0.7 * 1000;
		}
		if(!m_movement.jump && m_jumping){ //jump wordt losgelaten
			m_endJumpTime = SDL_GetTicks()+1; //jump is losgelaten
		}
		if( m_endJumpTime != 0 && SDL_GetTicks() >= m_endJumpTime){
			temp.y -= 5;
		}
	}else{
		//m_speed.y -= 5;
		m_endJumpTime = 0;
	}

	if(!m_intersecting)
		temp.y -= 10;

	
	Vector newVel = temp; //getVelocity();

	// Links/rechts/vooruit/achteruit bewegen
	if( m_movement.right )
		newVel.x += 100.f * frametime;

	if( m_movement.left )
		newVel.x -= 100.f * frametime;

	if( m_movement.backward )
		newVel.z += 100.f * frametime;

	if( m_movement.forward )
		newVel.z -= 100.f * frametime;


	// Wrijving.. zodat je een keer stopt met bewegen...
	newVel *= 1.f - frametime;

	// Maximale snelheid
	newVel.x = ( newVel.x > 0 ? 1 : -1 ) * MIN(fabs(newVel.x), 100 );
	newVel.y = ( newVel.y > 0 ? 1 : -1 ) * MIN(fabs(newVel.y), 200 );
	newVel.z = ( newVel.z > 0 ? 1 : -1 ) * MIN(fabs(newVel.z), 100 );
	setVelocity( newVel );

	EntityGroup::frame(frametime);

	m_intersecting = false;
}

void Player::init()
{
//romp
	Block* romp = new Block(Position(0,0,0), Dimensions(10,20,10), Color(0,0,1));
	//romp->setTexture(Texture("textures/sario3.png"));
	add(romp);

	//nek
	add(new Block(Position(0,12.5,0), Dimensions(5,5,5), Color(0.8,0.6,0.5)));

	//hoofd
	add(new Block(Position(0,16.5,0), Dimensions(8,8,8), Color(0.8,0.6,0.5)));
	//add(new Circle(Position(0,16.5,0), Dimensions(8,8,8), Color(0.8,0.6,0.5)));

	//pet
	add(new Block(Position(0,20,-3), Dimensions(9,2,15), Color(1,0,0)));

	//rechter bovenarm
	add(new Block(Position(-6.5,5,0), Dimensions(3,10,2), Color(1,0,0)));


	//linker bovenarm
	add(new Block(Position(6.5,5,0), Dimensions(3,10,2), Color(1,0,0)));
}

void Player::collisionResponse( AbstractEntity* other )
{
	Vector temp =  this->getVelocity();
	if(temp.y <5)
		temp.y = 0;
	if(this->getPosition().y < 20){
	if(other->getBoundingSize().x < other->getBoundingSize().z){
		//if(temp.x > 0 || temp.x < 0)
		temp.x = temp.x*-1;
	}
	if(other->getBoundingSize().x > other->getBoundingSize().z){
		//if(temp.z > 0 || temp.z < 0)
		temp.z = temp.z*-1;
	}
	if(other->getBoundingSize().x > other->getBoundingSize().y && other->getBoundingSize().z > other->getBoundingSize().y){
		if(temp.y <0 || !m_jumping)
		temp.y = 0;
	}
	}

	this->setVelocity(temp);
	m_jumping = false;
	m_intersecting = true;
}